﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Xcj.Mes.WholeLine.DataBase.Entity;
using Xcj.Mes.WholeLine.DataBase.Service;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.ViewModels.Production
{
    public class ProductionViewModel : BaseViewModel
    {
        private BatchEntity _production;
        public BatchEntity Production
        {
            get => _production;
            set
            {
                _production = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<BatchEntity> _batchEntities = new ObservableCollection<BatchEntity>();
        public ObservableCollection<BatchEntity> BatchEntities
        {
            get => _batchEntities;
            set
            {
                _batchEntities = value;
                RaisePropertyChanged();
            }
        }

        // 命令
        public ICommand ClearCommand { get; }
        public ICommand QueryCommand { get; }

        public ProductionViewModel()
        {
            Production = new BatchEntity();

            // 初始化命令
            ClearCommand = new RelayCommand(Clear);
            QueryCommand = new RelayCommand(Query);
        }

        /// <summary>
        /// 清空查询条件
        /// </summary>
        private void Clear()
        {
            Production = new BatchEntity();
            BatchEntities.Clear();
        }

        /// <summary>
        /// 执行查询操作
        /// </summary>
        private void Query()
        {
            try
            {
                // 验证时间是否填写
                if (Production.CreateTime == default(DateTime))
                {
                    MessageBox.Show("请选择生产时间！", "提示",
                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                // 使用BatchService进行查询
                using (BatchService service = new BatchService())
                {
                    var query = service.All().AsQueryable();

                    // 根据时间范围过滤（查询选定日期当天的数据）
                    var selectedDate = Production.CreateTime.Date;
                    var nextDate = selectedDate.AddDays(1);
                    query = query.Where(x => x.CreateTime >= selectedDate && x.CreateTime < nextDate);

                    if (!string.IsNullOrEmpty(Production.DeviceType))
                    { 
                        query = query.Where(x => x.DeviceType == Production.DeviceType);
                    }

                    // 根据料号过滤（不为空时）
                    if (!string.IsNullOrEmpty(Production.BatchNumber))
                    {
                        query = query.Where(x => x.BatchNumber != null &&
                            x.BatchNumber.Contains(Production.BatchNumber.Trim()));
                    }

                    // 根据批号过滤（不为空时）
                    if (!string.IsNullOrEmpty(Production.BatchName))
                    {
                        query = query.Where(x => x.BatchName != null &&
                            x.BatchName.Contains(Production.BatchName.Trim()));
                    }

                    var result = query.ToList();
                    BatchEntities = new ObservableCollection<BatchEntity>(result);

                    // 查询结果提示
                    if (!BatchEntities.Any())
                    {
                        MessageBox.Show("未找到符合条件的数据！", "提示",
                            MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询过程中发生错误：{ex.Message}", "错误",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 刷新数据（重新查询）
        /// </summary>
        public void RefreshData()
        {
            Query();
        }
    }
}
